package <%=packageName%>.service.impl.<%=context.extraInstance%>;

import <%=packageName%>.domain.<%=context.entityClass%>;
import <%=packageName%>.repository.<%=context.entityClass%>Repository;
import <%=packageName%>.service.<%=context.extraInstance%>.<%=context.entityClass%><%=context.extraName%>Service;
import <%=packageName%>.web.rest.util.PatchUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class <%=context.entityClass%><%=context.extraName%>ServiceImpl implements <%=context.entityClass%><%=context.extraName%>Service {

    private final <%=context.entityClass%>Repository <%=context.entityInstance%>Repository;

    public <%=context.entityClass%><%=context.extraName%>ServiceImpl(<%=context.entityClass%>Repository <%=context.entityInstance%>Repository) {
    this.<%=context.entityInstance%>Repository = <%=context.entityInstance%>Repository;
    }

    /**
     * 保存列表中的数据
     *
     * @param <%=context.entityInstancePlural%> 列表数据
     * @return 返回保存完毕的列表
     */
    @Override
    public List<<%=context.entityClass%>> save(List<<%=context.entityClass%>> <%=context.entityInstancePlural%>) {
        return <%=context.entityInstance%>Repository.saveAll(<%=context.entityInstancePlural%>);
    }

    /**
     * 保存单条数据，如果字段不存在则保持原来的数据不变
     *
     * @param tmpEntity 需要保存的数据
     * @return 返回保存完毕的数据
     */
    @Override
    public Optional<<%=context.entityClass%>> save(<%=context.entityClass%> tmpEntity) {
        if (tmpEntity.getId() == null) {
            return Optional.empty();
        }
        Optional<<%=context.entityClass%>> <%=context.entityInstance%>Optional = <%=context.entityInstance%>Repository.findById(tmpEntity.getId());
        <%=context.entityInstance%>Optional.ifPresent(<%=context.entityInstance%> -> PatchUtil.save(<%=context.entityInstance%>Repository, <%=context.entityInstance%>, tmpEntity));
        return <%=context.entityInstance%>Optional;
    }

    /**
     * 删除列表中的数据
     *
     * @param <%=context.entityInstancePlural%> 数据列表
     */
    @Override
    public void delete(List<<%=context.entityClass%>> <%=context.entityInstancePlural%>) {
       <%=context.entityInstance%>Repository.deleteAll(<%=context.entityInstancePlural%>);
    }
}
